app.controller("z-specialRequest-c", [ '$scope','$rootScope','$z-http','$sce','z-specialRequest-s','$filter',
		function($scope,$rootScope,$zhttp,$sce,Service,$filter) {
		$scope.viewModel = {};
		$scope.viewModel.perPage = "10";
		$scope.viewModel.rowCount = "1";		  
		$scope.viewModel.attendanceState=""       //考勤状态
		var now = new Date();                     //当前日期     
		var nowMonth = now.getMonth();            //当前月     
		var nowYear=$filter('date')(now,'yyyy');  //当前年
		//获取当月开始日期
		var monthStartDate = $filter("date")(new Date(nowYear, nowMonth, 1), "yyyy/MM/dd");
		//获取当前日期
        var dayEndDate= $filter("date")(now, "yyyy/MM/dd");
		$scope.viewModel.requestStartDate=new Date(monthStartDate);  //起止日期
		$scope.viewModel.requestEndDate=new Date(dayEndDate);
		//终止日期
		//日期控件
		$scope.viewModel.dateOptions = {
				formatYear: 'yy',
			    startingDay: 1,
			    showWeeks:false
		};
		//日期控件
		$scope.viewModel.format = "yyyy/MM/dd";
		//日期启动开关
		$scope.viewModel.startDateOpened = false;
		$scope.viewModel.endDateOpened = false;
		$scope.viewModel.editStartDateOpened = false;
		$scope.viewModel.editEndDateOpened = false;
		
		//
		$scope.editModel = {};
		$scope.editModel.request={};
		$scope.editModel.title="";
		
		//起止日期选择
		$scope.viewModel.startDateOpen = function($event) {
			$event.preventDefault();
			$event.stopPropagation();
			$scope.viewModel.startDateOpened = !$scope.viewModel.startDateOpened;
		};
		
		//起止日期选择
		$scope.viewModel.endDateOpen = function($event) {
			$event.preventDefault();
			$event.stopPropagation();
			$scope.viewModel.endDateOpened = !$scope.viewModel.endDateOpened;
		};
		
		$scope.viewModel.editStartDateOpen = function($event) {
			$event.preventDefault();
			$event.stopPropagation();
			$scope.viewModel.editStartDateOpened = !$scope.viewModel.editStartDateOpened;
		};
		
		$scope.viewModel.editEndDateOpen = function($event) {
			$event.preventDefault();
			$event.stopPropagation();
			$scope.viewModel.editEndDateOpened = !$scope.viewModel.editEndDateOpened;
		};
		//自定义列表信息
		$scope.viewModel.requestList = [{startDate:"2017/01/02",approvalState:"0",requestType:"事假",requestDate:"2017/01/25",approvalStateName:"待审批",description:"呼呼哈哈"},{startDate:"2017/01/03",approvalState:"0",requestType:"病假",requestDate:"2017/01/26",approvalStateName:"待审批",description:"呼呼哈哈"},{startDate:"2017/01/14",approvalState:"0",requestType:"婚假",requestDate:"2017/01/24",approvalStateName:"待审批",description:"呼呼哈哈"},{startDate:"2017/01/11",approvalState:"0",requestType:"调休",requestDate:"2017/01/20",approvalStateName:"待审批",description:"呼呼哈哈"}];
		//自定义申请类型信息
		$scope.viewModel.requestTypeData = [{dicCd:"0",dicName:"事假"},{dicCd:"1",dicName:"病假"},{dicCd:"2",dicName:"婚假"},{dicCd:"3",dicName:"调休"},{dicCd:"4",dicName:"产假"},{dicCd:"5",dicName:"陪护假"}]
		//加载数据
		$scope.viewModel.bindData = function(){
			$scope.viewModel.isLoading=true;
			//获取基本信息
//			Service.getSpecialRequestList({
//				pageNo:$scope.viewModel.rowCount,
//				pageCount:$scope.viewModel.perPage,
//				requestStartDate:$scope.viewModel.requestStartDate,
//				requestEndDate:$scope.viewModel.requestEndDate,
//				requestType:8
//			},
//			function(data){
				$scope.viewModel.dataList = $scope.viewModel.requestList;
//				$scope.viewModel.totalData = data.pageTotal;
//				$scope.viewModel.isLoading=false;
//			},function(error) {
//				$rootScope.alertError(error.fault.message);
//				$scope.viewModel.isLoading=false;
//			});
		};
		$scope.viewModel.bindData();
		
		//验证
		$scope.viewModel.validForm= function(isValid) {
            if (!isValid) {
                return;
            }else{
            	if($scope.viewModel.requestStartDate>$scope.viewModel.requestEndDate){
            		$scope.form.requestEndDate.$invalid=true;
            	}else{
            		$scope.form.requestEndDate.$invalid=false;
            		$scope.viewModel.rowCount=1;
                	$scope.viewModel.bindData();
            	}
            }
        };
        
		//加载
		$scope.viewModel.load=function(){
			$scope.viewModel.show=true;
			 //小时控件jquery-timepicker
		    $('#wStsp').hunterTimePicker('wStartTime',{
		        callback: function(time){
					$scope.editModel.request.startTime=time;
		        }
		    });
		    $('#wEdsp').hunterTimePicker('wEndTime',{
		        callback: function(time){
					$scope.editModel.request.endTime=time;
		        }
		    });
		};
		
		//关闭加载
		$scope.viewModel.unLoad=function(){
			$scope.viewModel.show=false;
			$scope.editModel.request={};
		};

		//新增
		$scope.viewModel.add = function(request){
			$scope.viewModel.load();
			$scope.editModel.title="新增特殊情况申请";
			$scope.editModel.type = true;
			$scope.editModel.request={};
			//验证不能为空
			$scope.editForm.startDate.$dirty=false;
			$scope.editForm.description.$dirty=false;
		};
		
		//修正
		$scope.viewModel.update=function(request){
			$scope.viewModel.load();
			$scope.editModel.title="修改加班申请";
			$scope.editModel.type = false;
			$scope.editModel.request=angular.copy(request);
			$scope.editModel.request.startDate=new Date(request.startDate);
		};
		
		//提交
		$scope.viewModel.submit=function(request){
			$scope.editModel.request=angular.copy(request);
			request.approvalState=1;
			$scope.editModel.request.startDate=new Date(request.startDate);
			$scope.viewModel.isLoading = true;
			$scope.viewModel.isDisabled(request);
//			Service.updateWorkRequest($scope.editModel.request,
//					function(data){
						$scope.viewModel.unLoad();
						$scope.viewModel.bindData();
						$rootScope.alertInfo("提交成功。");
						$scope.viewModel.isLoading = false;
//					},function(error) {
//						$rootScope.alertError(error.fault.message);
//						$scope.viewModel.isLoading = false;
//					});
		};

		//验证
		$scope.viewModel.requiredForm= function() {
            if (!$scope.editForm.$valid) {
            	if(!$scope.editForm.startDate.$dirty){
            		$scope.editForm.startDate.$dirty=true;
            	}
            	if(!$scope.editForm.requestType.$dirty){
            		$scope.editForm.requestType.$dirty=true;
            	}
            	if(!$scope.editForm.description.$dirty){
            		$scope.editForm.description.$dirty=true;
            	}
                return true;
            }else{
            	return false;
            }
        };
		
		$scope.viewModel.saveDate = function(key,request){
			if($scope.viewModel.requiredForm()){
				return;
			}
			$scope.viewModel.isLoading = true;
			if($scope.editModel.type){
//				Service.saveSpecialRequest($scope.editModel.request,
//						function(data){
//							$scope.viewModel.unLoad();
//							$scope.viewModel.bindData();
//							$rootScope.alertInfo("新增成功。");
//							$scope.viewModel.isLoading = false;
//						},function(error) {
//							$rootScope.alertError(error.fault.message);
//							$scope.viewModel.isLoading = false;
//						});
				$scope.viewModel.requestList.push($scope.editModel.request);
				$scope.viewModel.unLoad();
				$scope.viewModel.bindData();
				$rootScope.alertInfo("新增成功。");
				$scope.viewModel.isLoading = false;
			}else{
//				Service.updateSpecialRequest($scope.editModel.request,
//						function(data){
//							$scope.viewModel.unLoad();
//							$scope.viewModel.bindData();
//							$rootScope.alertInfo("修改成功。");
//							$scope.viewModel.isLoading = false;
//						},function(error) {
//							$rootScope.alertError(error.fault.message);
//							$scope.viewModel.isLoading = false;
//						});
			}
		}
		
		//保存
		$scope.viewModel.save = function(){
			$scope.editModel.request.approvalState=0;
			$scope.viewModel.saveDate();
		};
		
        $scope.viewModel.saveSubmit = function(){
        	$scope.editModel.request.approvalState=1;
        	$scope.viewModel.saveDate();
        };
        
		//删除
		$scope.viewModel.del=function(key,request){
			$rootScope.alertConfirm("确定删除吗？",function() {
				$scope.viewModel.isLoading=true;
//				Service.deleteRequest({
//					requestId:request.requestId
//				},function(){
//					$scope.viewModel.bindData();
//					$scope.viewModel.isLoading=false;
//					$rootScope.alertInfo("删除成功。");
//				},function(error){
//					$scope.viewModel.isLoading=false;
//					$rootScope.alertError(error.fault.message);
//				});
				$scope.viewModel.requestList.splice(key,1);
				$scope.viewModel.bindData();
				$scope.viewModel.isLoading=false;
				$rootScope.alertInfo("删除成功。");
			});
		};
		
		//时间框失去焦点
		$scope.viewModel.dateChange=function(){
			if($scope.editModel.request.startDate!=null
					&& $scope.editModel.request.endDate!=null
					&& $scope.editModel.request.startTime!=null
					&& $scope.editModel.request.endTime!=null){
				var end ='2000-01-01 '+$scope.editModel.request.endTime;
				var endTime = new Date(end.replace(/-/g,"/") );
				var start ='2000-01-01 '+$scope.editModel.request.startTime;
				var startTime = new Date(start.replace(/-/g,"/") );
				var sum1=$scope.editModel.request.endDate-$scope.editModel.request.startDate;
				var sum2=endTime-startTime;
				var sum = (sum1+sum2)/3600000;
				$scope.editModel.request.hours=sum.toFixed(1);
			}else{
				$scope.editModel.request.hours=0;
			}
		};
		
		//按钮是否可用
		$scope.viewModel.isDisabled=function(request){
			if(request.approvalState==1){
        		return true;
        	}else{
        		return false;
        	}
		};
} ]);